package service

import (
	"k8s-platform/dao"
	"k8s-platform/model"
)

var Workflow workflow

type workflow struct{}




//新增workflow
type WorkflowCreate struct {
	Name string `json:"name"`
	Namespace string `json:"namespace"`
	Replicas int32 `json:"replicas"`
	Image string `json:"image"`
	Label map[string]string `json:"label"`
	Cpu string `json:"cpu"`
	Memory string `json:"memory"`
	ContainerPort int32 `json:"container_port"`
	HealthCheck bool `json:"health_check"`
	HealthPath string `json:"health_path"`
	Type string `json:"type"`
	Port int32 `json:"port"`
	NodePort int32 `json:"node_port"`
	Hosts map[string][]*HttpPath `json:"hosts"`
}
//workflow名字转换成service名字  添加-svc后缀方便区分
func getIngressName(workflowName string) (serviceName string) {
	return workflowName + "-ing"
}
func getServiceName(workflowName string) (serviceName string) {
	return workflowName + "-svc"
}

//封装workflow对应的k8s资源
func createWorkflowRes(data *WorkflowCreate) (err error){
	//声明service类型
	var serviceType string
	//组装deployCreate数据
	dc := &DeployCreate{
		Name:          data.Name,
		Namespace:     data.Namespace,
		Replicas:      data.Replicas,
		Image:         data.Image,
		Label:         data.Label,
		Cpu:           data.Cpu,
		Memory:        data.Memory,
		ContainerPort: data.ContainerPort,
		HealthCheck:   data.HealthCheck,
		HealthPath:    data.HealthPath,
	}
	//创建deployment
	err =Deployment.CreateDeployment(dc)
	if err != nil {
		return err
	}
	//判断service类型
	if data.Type != "Ingress"{
		serviceType = data.Type
	} else {
		serviceType = "ClusterIP"
	}
	//组装serviceCreate类型数据
	sc := &ServiceCreate{
		Name:          getServiceName(data.Name),
		Namespace:     data.Namespace,
		Type:          serviceType,
		ContainerPort: data.ContainerPort,
		Port:          data.Port,
		NodePort:      data.NodePort,
		Label:         data.Label,
	}
	//创建service
	err = Service.CreateService(sc)
	if err != nil {
		return err
	}
	//组装IngressCreate类型的数据，创建ingress
	if data.Type == "Ingress" {
		ic := &IngressCreate{
			Name:      getIngressName(data.Name),
			Namespace:  data.Namespace,
			Label:     data.Label,
			Hosts:     data.Hosts,
		}
		//创建ingress
		err = Ingress.CreateIngress(ic)
		if err != nil {
			return err
		}
	}
	return nil
}

//创建workflow工作流
func (w *workflow)CreateWorkFlow(data *WorkflowCreate) (err error) {
	//若workflow不是ingress类型传入空字符串
	var ingressName string
	if data.Type == "Ingress" {
		ingressName = getIngressName(data.Name)
	}else {
		ingressName = ""
	}
	//组装mysql中的workflow的单条数据
	workflow := &model.WorKflow{
		Name:       data.Name,
		Namespace:  data.Namespace,
		Replicas:   data.Replicas,
		Deployment: data.Name,
		Service:    getServiceName(data.Name),
		Ingress:    ingressName,
		Type:       data.Type,
	}
	//调用dao层执行数据库的添加操作
	err = dao.Workflow.Add(workflow)
	if err != nil {
		return err
	}
	//创建k8s资源
	err = createWorkflowRes(data)
	if err != nil {
		return err
	}
	return nil
}

//删除workflow
func (w *workflow)DelWorkById(id int) (err error)  {
	//获取数据库数据
	workflow, err := dao.Workflow.GetById(id)
	if err != nil {
		return nil
	}
	//删除k8s资源
	err = delWorkflowRes(workflow)
	if err != nil {
		return nil
	}
	//删除数据库数据
	err = dao.Workflow.DelById(id)
	if err != nil {
		return nil
	}
	return nil
}

//删除workflow对应的k8s资源 deploy service ingress
func delWorkflowRes(workflow *model.WorKflow) (err error) {
	err = Deployment.DeleteDeploy(workflow.Name,workflow.Namespace)
	if err != nil {
		return nil
	}
	err = Service.DeleteService(getServiceName(workflow.Name),workflow.Namespace)
	if err != nil {
		return nil
	}
	if workflow.Type == "Ingress" {
		err = Ingress.DeleteIngress(getIngressName(workflow.Name),workflow.Namespace)
		if err != nil {
			return nil
		}
	}
	return nil
}




//获取列表分页查询
func (w *workflow) GetList(name string, page, limit int) (data *dao.WorkFlowResp, err error) {
	data, err = dao.Workflow.GetList(name, page, limit)
	if err != nil {
		return nil, err
	}
	return data,nil
}
//获取workflow详情
func (w *workflow)GetById(id int)(data *model.WorKflow,err error) {
	data, err = dao.Workflow.GetById(id)
	if err != nil {
		return nil, err
	}
	return data,nil
}






















